package main

import (
	"fmt"
	"io"
	"log"
	"net/http"
	"os"
	"os/signal"
	//	"time"

	"github.com/astaxie/beego"
)

type HomeController struct {
	beego.Controller
}

func (this *HomeController) Get() {
	this.Ctx.WriteString("hello beego")
}

func sayHello1(rw http.ResponseWriter, req *http.Request) {
	io.WriteString(rw, "Hello world, this is version 1.")
}
func sayHello2(rw http.ResponseWriter, req *http.Request) {
	io.WriteString(rw, "Hello world, this is version 2.")
}

type myhandler struct {
}

var mux map[string]func(w http.ResponseWriter, r *http.Request)

func (*myhandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	if h, ok := mux[r.URL.String()]; ok {
		h(w, r)
		return
	}
	io.WriteString(w, "URL:"+r.URL.String())
}

func sayHello(w http.ResponseWriter, r *http.Request) {
	io.WriteString(w, "hello!")
}
func sayBye(w http.ResponseWriter, r *http.Request) {
	io.WriteString(w, "Bye bye!")
}
func main() {
	/*
		fmt.Println("----------------beego router---------------------")
		beego.Router("/", &HomeController{})
		beego.Run(":80")
	*/
	/*
		fmt.Println("----------------HandleFunc hello version1---------------------")
		//设置路由
		http.HandleFunc("/", sayHello)
		err := http.ListenAndServe(":80", nil)
		if err != nil {
			log.Fatal(err)
		}
	*/
	/*
		fmt.Println("----------------mux url---------------------")
		mux := http.NewServeMux()
		mux.Handle("/", &myhandler{})
		mux.HandleFunc("/func", sayHello2)
		http.ListenAndServe(":80", mux)
	*/
	/*
		fmt.Println("----------------server anonymous function hello version 3---------------------")
		amux := http.NewServeMux()
		amux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
			io.WriteString(w, "Hello world, this is version 3.")
		})
		(&http.Server{
			Addr:        ":80",
			Handler:     amux,
			ReadTimeout: 5 * time.Second, //读超时
		}).ListenAndServe()
	*/
	/*
		fmt.Println("----------------mux map router---------------------")
		mux = make(map[string]func(w http.ResponseWriter, r *http.Request), 2)
		mux["/hello"] = sayHello
		mux["/bye"] = sayBye
		(&http.Server{
			Addr:        ":80",
			Handler:     &myhandler{},
			ReadTimeout: 5 * time.Second, //读超时
		}).ListenAndServe()
	*/
	/*
		fmt.Println("----------------static file server under dir http---------------------")
		mux := http.NewServeMux()
		wd, err := os.Getwd()
		if err != nil {
			log.Fatal(err)
		}
		mux.Handle("/http/", http.StripPrefix("/http/", http.FileServer(http.Dir(wd))))
		http.ListenAndServe(":80", mux)
	*/
	fmt.Println("----------------version equal or greater than 1.8  signal---------------------")
	mux := http.NewServeMux()
	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("hello world!"))
	})
	server := &http.Server{
		Addr:    ":80",
		Handler: mux,
	}

	quit := make(chan os.Signal)
	signal.Notify(quit, os.Interrupt)
	go func() {
		<-quit
		if err := server.Close(); err != nil {
			log.Fatal("Close server:", err)
		}
	}()
	if err := server.ListenAndServe(); err != nil {
		if err == http.ErrServerClosed {
			log.Println("Server closed under request")
		} else {
			log.Fatal("Server closed unexpected")
		}
	}
	log.Println("Server exit")
}
